//
//  WQStatus.swift
//  WeiBo
//
//  Created by 张德华 on 7/25/16.
//  Copyright © 2016 ZhangDeHua. All rights reserved.
//

import UIKit

class WQStatus: NSObject {

    /**微博创建时间*/
    var created_at: String?
    
    /**微博信息内容*/
    var text: String?
    
    /**微博来源*/
    var source: String? {
        didSet{
            sourceAttr = getSourceStr(source)
        }
    }
    
    /**微博ID*/
    var id: Int64 = 0
    
    
    /**转发数*/
    var reposts_count: Int = 0
    /**评论数*/
    var comments_count: Int = 0
    /**表态数*/
    var attitudes_count: Int = 0
    
    /**用户数据*/
    var user: WQUser?
    /**转发微博*/
    var retweeted_status: WQStatus?
    /**配图*/
    var pic_urls: [WQPictureInfo]?
    
    /**微博来源处理富文本*/
    var sourceAttr: NSMutableAttributedString?
    
    
    // MARK: - ************kvc构造函数
    init(dict: [String: AnyObject]) {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    
    // MARK: - ************嵌套的模型kvc
    override func setValue(value: AnyObject?, forKey key: String) {
        
        ///------ user
        if key == "user" {
            
            guard let v = value as? [String: AnyObject] else {
                return
            }
            user = WQUser(dict: v)
            
        } else if key == "retweeted_status" {
            
            guard let v = value as? [String: AnyObject] else {
                return
            }
            retweeted_status = WQStatus(dict: v)
            
        } else if key == "pic_urls" {
            
            
            guard let v = value as? [[String: AnyObject]] else {
                return
            }
            
            var tempArray: [WQPictureInfo] = [WQPictureInfo]()
            for dict in v {
                
                let pic = WQPictureInfo(dict: dict)
                tempArray.append(pic)
            }
            
            pic_urls = tempArray
        } else {
            
            super.setValue(value, forKey: key)
        }
    }
    
    
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {}
    
    
    
    override var description: String {
        
        let keys = ["created_at","text","source","id"]
        
        return dictionaryWithValuesForKeys(keys).description
        
    }
    
    
}

extension WQStatus {
    
    ///------ 格式
    func getSourceStr(source: String?) -> NSMutableAttributedString {
        
        ///------ 判断是否为nil,而且是否保存
        guard let s = source where s.containsString("\">") else {
            
            let str = "上海三期微博"
            return getSourceAttr(str)
        }
        
        ///------ 起始位置和结束位置
        let startRange = s.rangeOfString("\">")!
        let endRange = s.rangeOfString("</")!
        
        ///------ 字符串截取
        let result = s.substringWithRange(startRange.endIndex..<endRange.startIndex)
        
        return getSourceAttr(result)
    }
    
    func getSourceAttr(result: String) -> NSMutableAttributedString{
        
        ///------ 最终显示格式
        let name = "来自" + result
        ///------ 得到range
        let range = (name as NSString).rangeOfString("来自")
        ///------ 创建富文本
        let attr = NSMutableAttributedString(string: name)
        ///------ 设置属性
        attr.addAttributes([NSForegroundColorAttributeName: UIColor.grayColor()], range: range)
        
        return attr
    }
}

























